<?php

class Home extends Controller {
	
	function index() {
            $this->set('title','YOU - Welcome');
	}
	
	function register(){
            global $session;

            $session->set('status',0);
            $field = array();
            $field['fullname'] = $_POST['fnama'];
            $field['username'] = $_POST['fuser'];
            $field['password'] = $_POST['fpass'];
            $field['tgllahir'] = $_POST['ftanggal'];
            $field['email'] = $_POST['femail'];
            $field['kota'] = $_POST['flokasi'];
            $field['gender'] = $_POST['fgender'];
            $field['pp'] = $_FILES['fprofpict'];
            $result = $this->HomeModel->query('select username from user where Username=\''.mysql_real_escape_string($field['username']).'\'');
            if(count($result) != 0){
                $this->_auto_render = false;
                $this->set('title','Register Failed');
            } else {
                $session->set('status',1);
                $session->set('fullname',$field['fullname']);
                $this->HomeModel->insert($field);
                $session->set('userid',  mysql_insert_id());
                $field['iduser'] = mysql_insert_id();
                $this->HomeModel->insertwall($field);
                $this->_auto_render = false;
                $this->redirect(BASE_PATH.'home2');
            }
	}
	
	function login(){
            global $session;

            $session->set('status',0);
            $field = array();
            $field['username'] = $_POST['luser'];
            $field['password'] = $_POST['lpass'];

            $result = $this->HomeModel->query('select * from user where Username=\''.mysql_real_escape_string($field['username']).'\' and password=\''.mysql_real_escape_string($field['password']).'\'');
            if(count($result) != 0){
                //success login
                //print_r("Login berhasil");
                $this->_auto_render = false;
                $this->HomeModel->setLogin($result[0]['User']['iduser']);
                $this->redirect(BASE_PATH.'home2');
                $session->set('userid',$result[0]['User']['iduser']);
                //$this->set('title',$result[0]['User']['fullname']);
                //ini ngeset variabel session dengan nama userid sesuai dengan value di parameter kedua
                //$session->set('userid',$result[0]['User']['idUser']);
                //ini buat dapetin value yang disimpan di session dengan nama userid
                //$session->get('userid');
                //fungsi lainnya lo liat di folder application/session.classs.php intinya kalo mau make harus deklarasiin global $session dulu di fungsi
                //print_r($session);
            }else{
                //failed login
                $this->_auto_render = false;
                $this->set('title','Login Failed');
                //$this->redirect('http://localhost/YOU/home/index');
            }
//            $this->redirect('http://localhost/home2/index');
//                $this->redirect('index');
	}
	
	function beforeAction(){
		$this->set('title',null);
	}
	

}
